﻿using RestSharp;
using RestSharp.Authenticators;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UI
{
  public   class PmsApi
    {

        // TwilioApi.cs
        
           //const string BaseUrl = "http://localhost:8090/api/";
           // const string BaseUrl = Comm.TempData.BaseUrl;
            RestClient client = new RestClient(new Uri(Comm.TempData.BaseUrl));
            readonly string _accountSid;
            readonly string _secretKey;

            public PmsApi()
            {
                //_accountSid = accountSid;
                //_secretKey = secretKey;
            }

        //public T Execute<T>(RestRequest request) where T : new()
        //{
        //    var client = new RestClient();
        //    client.BaseUrl = new System.Uri(BaseUrl);
        //    //client.Authenticator = new HttpBasicAuthenticator(_accountSid, _secretKey);
        //    //request.AddParameter("AccountSid", _accountSid, ParameterType.UrlSegment); // used on every request
        //    var response = client.Execute<T>(request);

        //    if (response.ErrorException != null)
        //    {
        //        const string message = "Error retrieving response.  Check inner details for more info.";
        //        var twilioException = new ApplicationException(message, response.ErrorException);
        //        throw twilioException;
        //    }
        //    return response.Data;
        //}



        public async Task<List<T>>  GetList<T>(RestRequest request) where T : new()
        {
                       
            var response = await client.GetAsync<ListDataResponse<T>>(request);

            return response.data;
       
        }

        public async Task<T> Get<T>(RestRequest request) where T : new()
        {

            var response = await client.GetAsync<SingleDataResponse<T>>(request);
            return response.data;

        }

        public async Task<bool> Put<T>(RestRequest request) where T : new()
        {

            var response = await client.PutAsync<SingleDataResponse<bool>>(request);
            return response.data;

        }


        public async Task<int> Post<T>(RestRequest request) where T : new()
        {

            var response = await client.PostAsync<SingleDataResponse<int>>(request);
            return response.data;

        }

        public async Task<bool> Delete<T>(RestRequest request) where T : new()
        {
            var response = await client.DeleteAsync<SingleDataResponse<bool>>(request);

            if(response.succeeded)
            {
                return response.data;
            }else
            {
                return false;
            }
           

        }
        public async Task<T> ExecuteAsync<T>(RestRequest request) where T :new ()
        {
            var response = await client.ExecuteAsync<T>(request);
            return response.Data;

        }
    }
}
